unit usrmng;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzButton, Grids, RzGrids, CheckLst;

type
  Tusrmngfrm = class(TForm)
    Label1: TLabel;
    usrlist: TRzStringGrid;
    Label2: TLabel;
    txtusrName: TEdit;
    Label3: TLabel;
    txtusrPswd: TEdit;
    RzBitBtn1: TRzBitBtn;
    RzBitBtn2: TRzBitBtn;
    RzBitBtn3: TRzBitBtn;
    RzBitBtn4: TRzBitBtn;
    Label4: TLabel;
    chklstPower: TCheckListBox;
    procedure RzBitBtn4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure usrlistDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure usrlistClick(Sender: TObject);
    procedure RzBitBtn1Click(Sender: TObject);
    procedure RzBitBtn2Click(Sender: TObject);
    procedure RzBitBtn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure refreshdb;
  end;

var
  usrmngfrm: Tusrmngfrm;

implementation

{$R *.dfm}

uses
  IncludeFile, dbmodule;

procedure Tusrmngfrm.refreshdb;
var
  i:integer;
begin
  i:=0;
  with dbmodulefrm.dbtmpqry do begin
    Active:=False;
    SQL.Clear;
    SQL.Add('Select * from SYS_USER');
    ExecSQL;
    open;
    usrlist.RowCount:=Recordcount+1; 
    First;
    while not eof do begin
      inc(i);
      usrlist.Cells[0,i]:=FieldByName('NAME').AsString;
      usrlist.Cells[1,i]:=FieldByName('PASWD').AsString;
      usrlist.Cells[2,i]:=FieldByName('POWER').AsString;
      usrlist.Cells[3,i]:=FieldByName('TYPE').AsString;
      next;
    end;
    close;
  end;
  usrlistClick(self);
end;

procedure Tusrmngfrm.RzBitBtn4Click(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

procedure Tusrmngfrm.FormCreate(Sender: TObject);
var
  I:integer;
begin
  inherited;
  Caption:=GetCaption(name,1000);
  for I:=0 to ComponentCount-1 do begin
    if Components[I] is TLabel then begin
      if 0<>TLabel(Components[I]).Tag then
        TLabel(Components[I]).Caption:=
                GetCaption(name,TLabel(Components[I]).Tag);
    end;
    if Components[I] is TRzBitBtn then begin
      if 0<>TRzBitBtn(Components[I]).Tag then
        TRzBitBtn(Components[I]).Caption:=
                GetCaption(name,TRzBitBtn(Components[I]).Tag);
    end;
  end;
  usrlist.ColWidths[2]:=-1;
  usrlist.ColWidths[3]:=-1;  
  usrlist.Cells[0,0]:='User Name';
  usrlist.Cells[1,0]:='Password';
end;

procedure Tusrmngfrm.usrlistDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  vCol, vRow : LongInt;
  tW,tH:LongInt;
begin
  vCol := ACol; vRow := ARow;
  With Sender as TStringGrid, Canvas do begin
    tW:=(Rect.Right-Rect.Left-TextWidth(Cells[vCol, vRow])) div 2;
    tH:=(Rect.Bottom-Rect.Top-TextHeight('A')) div 2;
    FillRect(Rect);
    if vRow=0 then begin
      Font.Style :=Font.Style+[fsBold];
      //TextRect(Rect, Rect.Left+tW, Rect.Top+tH, Cells[vCol, vRow]);
    end else begin
      Font.Style :=Font.Style-[fsBold];
    end;
    TextRect(Rect, Rect.Left+tW, Rect.Top+tH, Cells[vCol, vRow]);
  end;

end;

procedure Tusrmngfrm.usrlistClick(Sender: TObject);
var
  tmp:string;
  I:integer;
begin
  with usrlist do begin
    txtusrName.Text:=Cells[0,Row];
    txtusrPswd.Text:=Cells[1,Row];
    tmp:=Trim(Cells[2,Row]);
  end;
  for i:=1 to 16 do begin
    if 'Y'=tmp[i] then
      chklstPower.Checked[i-1]:=True
    else
      chklstPower.Checked[i-1]:=False;
  end;
end;

procedure Tusrmngfrm.RzBitBtn1Click(Sender: TObject);
var
  tmp:string;
  i:integer;
begin
  tmp:='';
  for i:=0 to 15 do begin
    if chklstPower.Checked[i] then
      tmp:=tmp+'Y'
    else
      tmp:=tmp+'N'
  end;
  try
    with dbmodulefrm.dbtmpqry do begin
      Active:=False;
      SQL.Clear;
      SQL.Add('Insert into SYS_USER');
      SQL.Add(Format('(NAME,PASWD,POWER) VALUES (''%s'',''%s'',''%s'')',
                             [txtusrName.text,txtusrPswd.text,tmp]));
      ExecSQL;
    end;
  except
    txtusrName.text:='';
    txtusrPswd.Text:='';
    Application.MessageBox(pchar(getCaption('Msgbox',114)),
                           pchar(getCaption('Msgbox',101)),
                           48);
  end;
  refreshdb;
end;

procedure Tusrmngfrm.RzBitBtn2Click(Sender: TObject);
var
  tmp:string;
begin
  if 0=strtoint(usrlist.Cells[3,usrlist.Row]) then begin
    Application.MessageBox(pchar(getCaption('Msgbox',115)),
                           pchar(getCaption('Msgbox',101)),
                           64);
    exit;
  end;
  tmp:=Trim(usrlist.Cells[0,usrlist.Row]);
  with dbmodulefrm.dbtmpqry do begin
    Active:=False;
    SQL.Clear;
    SQL.Add('DELETE FROM SYS_USER');
    SQL.Add(Format('WHERE NAME=''%s''',[tmp]));
    ExecSQL;
  end;
  refreshdb;
end;

procedure Tusrmngfrm.RzBitBtn3Click(Sender: TObject);
var
  tmp:string;
  i:integer;
begin
  tmp:='';
  for i:=0 to 15 do begin
    if chklstPower.Checked[i] then
      tmp:=tmp+'Y'
    else
      tmp:=tmp+'N'
  end;
  try
    with dbmodulefrm.dbtmpqry do begin
      Active:=False;
      SQL.Clear;
      SQL.Add('Update SYS_USER');
      SQL.Add(Format('Set [Name]=''%s'',PASWD=''%s'',POWER=''%s''',
                             [txtusrName.text,txtusrPswd.text,tmp]));
      SQL.Add(Format('Where [NAME]=''%s''',[usrlist.Cells[0,usrlist.Row]]));
      ExecSQL;
    end;
  except
  end;
  refreshdb;
end;

end.
